package com.example.ten.article;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.ten.R;
import com.example.ten.base.BaseFragment;
import com.example.ten.utils.ContentUtils;
import com.google.gson.Gson;

/**
 * A simple {@link Fragment} subclass.
 */
public class ItemArticleFragment extends BaseFragment {
    TextView titleTv,authorTv1,timesTv,summaryTv,textTv,authorTv2,authorbriefTv;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.fragment_item_article, container, false);
        initView(view);
        Bundle bundle = getArguments();
        ArticleBean.ResultBean resultBean = (ArticleBean.ResultBean) bundle.getSerializable("article");
//        titleTv.setText(resultBean.getTitle());
//        summaryTv.setText(resultBean.getSummary());

//        开始网络请求，获取网络数据，并解析
        loadData(getContext(), ContentUtils.ARTICLE_DETATLS_URL+resultBean.getId(),
                "",false,false,true);
        return view;
    }
    /* 初始化控件*/
    private void initView(View view) {
        titleTv = view.findViewById(R.id.itemarticle_tv_title);
        authorTv1 = view.findViewById(R.id.itemarticle_tv_author1);
        timesTv = view.findViewById(R.id.itemarticle_tv_times);
        summaryTv = view.findViewById(R.id.itemarticle_tv_summary);
        textTv = view.findViewById(R.id.itemarticle_tv_text);
        authorTv2 = view.findViewById(R.id.itemarticle_tv_author2);
        authorbriefTv = view.findViewById(R.id.itemarticle_tv_authorbrief);
    }

    @Override
    public void showDataFromNet(String s) {
//        解析数据
        ArticleDetailsBean bean = new Gson().fromJson(s, ArticleDetailsBean.class);
        titleTv.setText(bean.getTitle());
        authorTv1.setText("作者:"+bean.getAuthor());
        timesTv.setText("阅读量:"+bean.getTimes());
        summaryTv.setText(bean.getSummary());
        textTv.setText(bean.getText());
        authorTv2.setText(bean.getAuthor());
        authorbriefTv.setText(bean.getAuthorbrief());
    }
}
